package jp.dip.ysato.excite;

import jp.dip.ysato.excite.R;
import jp.dip.ysato.excite.R.id;
import jp.dip.ysato.excite.R.layout;
import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class LoginActivity extends Activity {
	
	private Button okbutton;
	private Button cancelbutton;
	private EditText user;
	private EditText pass;

	private final OnClickListener onclick = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			Intent intent = new Intent();
			if (okbutton.equals(arg0)) {
				DBHelper dbhelper = new DBHelper(LoginActivity.this);
				SQLiteDatabase db = dbhelper.getWritableDatabase();
				db.execSQL("DELETE FROM account");
				String u = user.getText().toString();
				String p = pass.getText().toString();
				ContentValues cv = new ContentValues();
				cv.put("user", u);
				cv.put("password", p);
				db.insert("account", null, cv);
				db.close();
				setResult(RESULT_OK, intent);
			} else if (cancelbutton.equals(arg0)) {
				setResult(RESULT_CANCELED, intent);
			}
			finish();
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		okbutton = (Button) findViewById(R.id.ok_button);
		cancelbutton = (Button) findViewById(R.id.cancel_button);
		user = (EditText) findViewById(R.id.editText1);
		pass = (EditText) findViewById(R.id.editText2);
		okbutton.setOnClickListener(onclick);
		cancelbutton.setOnClickListener(onclick);
	}
}
